<!--
 * @Author: your name
 * @Date: 2021-06-01 16:19:10
 * @LastEditTime: 2021-06-01 16:41:51
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue3_study\vue-cli-test\组合api\响应式的基本原理.html
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 目标对象
        const user = {
            name: '张三',
            age: 20,
            wife: {
                name: '李四',
                age: 18
            }
        }

        // 将目标对象变成代理对象
        // 参数1：user ====> target目标对象
        // 参数2：handler ====> 处理器对象，用来监控数据和对数据操作
        const proxyUser = new Proxy(user, {
            get(target, prop) {
                console.log("get方法调用了");
                return Reflect.get(target, prop)
            },
            set(target, prop, val) {
                console.log('set方法调用了');
                return Reflect.set(target, prop, val)
            },
            deleteProperty(target, prop) {
                console.log('deleteProperty方法调用了');
                return Reflect.deleteProperty(target, prop)
            }
        })

        // 通过代理对象获取目标对象中的某个属性值
        console.log(proxyUser.name);
        // 通过代理对象修改目标对象中的某个属性值
        proxyUser.name = '王五'
        console.log(proxyUser.name);
        proxyUser.gender = '男'
        console.log(proxyUser);
        // 通过代理对象删除目标兑现公众的某个属性值
        delete proxyUser.gender
        console.log(proxyUser);
        proxyUser.wife.age = 40
        console.log(proxyUser);
    </script>
</body>

</html>